package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.back.WenzhangBack;
import com.ruoyi.information.mapper.back.RecomBackMapper;
import com.ruoyi.information.mapper.back.WenzhangBackMapper;
import com.ruoyi.information.service.back.IWenzhangBackService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务案例Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-04
 */
@Service
public class WenzhangBackServiceImpl implements IWenzhangBackService {
    @Autowired
    private WenzhangBackMapper wenzhangMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RecomBackMapper recomBackMapper;

    /**
     * 查询服务案例
     *
     * @param id 服务案例主键
     * @return 服务案例
     */
    @Override
    public WenzhangBack selectWenzhangById(Long id) {
        return wenzhangMapper.selectWenzhangById(id);
    }

    /**
     * 查询服务案例列表
     *
     * @param wenzhang 服务案例
     * @return 服务案例
     */
    @Override
    public List<WenzhangBack> selectWenzhangList(WenzhangBack wenzhang) {
        return wenzhangMapper.selectWenzhangList(wenzhang);
    }

    /**
     * 新增服务案例
     *
     * @param wenzhang 服务案例
     * @return 结果
     */
    @Override
    public int insertWenzhang(WenzhangBack wenzhang) {
        wenzhang.setCreateTime(DateUtils.getNowDate());
        return wenzhangMapper.insertWenzhang(wenzhang);
    }

    /**
     * 修改服务案例
     *
     * @param wenzhang 服务案例
     * @return 结果
     */
    @Override
    public int updateWenzhang(WenzhangBack wenzhang) {
        wenzhang.setUpdateTime(DateUtils.getNowDate());
        /*删除云资源*/
        if (wenzhang.getCoverResource() != null) {
            WenzhangBack main = wenzhangMapper.selectWenzhangById(wenzhang.getId());
            if (main.getCoverResource() != null) {
                CloudClassroomBackServiceImpl.comparePhoto(main.getCoverResource(), wenzhang.getCoverResource(), remoteFileService);
            }
        }
        return wenzhangMapper.updateWenzhang(wenzhang);
    }

    /**
     * 批量删除服务案例
     *
     * @param ids 需要删除的服务案例主键
     * @return 结果
     */
    @Override
    public int deleteWenzhangByIds(Long[] ids) {
        /*删除云资源*/
        List<String> photos = wenzhangMapper.selectWenzhangListPhotos(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        recomBackMapper.deleteRecomByIdsAndType(ids,"服务案例");
        return wenzhangMapper.deleteWenzhangByIds(ids);
    }

    /**
     * 删除服务案例信息
     *
     * @param id 服务案例主键
     * @return 结果
     */
    @Override
    public int deleteWenzhangById(Long id) {
        return wenzhangMapper.deleteWenzhangById(id);
    }
}
